package com.fjc.security;

import java.security.MessageDigest;

/**
 * MD5加密工具
 * Created by fjc on 2018/4/7.
 */
public class MD5 {
    public static String MD5(String s) {
        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(s.getBytes("utf-8"));
            return toHex(bytes);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    private static String toHex(byte[] bytes) {

        final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (int i=0; i<bytes.length; i++) {
            ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
            ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
        }
        return ret.toString();
    }

    public static void main(String[] args) throws Exception{
        MD5 m = new MD5();
        String a = m.MD5("11");
        System.out.println(a);
    }
}
